<template>
  <div id="Stock">
    <!-- 查询区域 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="仓库名">
        <el-input v-model="searchForm.stockName" placeholder="请输入仓库名"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input placeholder="请输入商品名称" v-model="searchForm.productName"></el-input>
      </el-form-item>
      <el-form-item label="供应商名称">
        <el-input placeholder="请输入供应商名称" v-model="searchForm.companyName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="编号" width="100" align="center"></el-table-column>
      <el-table-column prop="image" label="图片" width="200" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.image" alt="商品图片" style="width: 50px; height: 50px;">
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="商品名称" width="300" align="center"></el-table-column>
      <el-table-column prop="warehouseName" label="存放仓库" width="300" align="center"></el-table-column>
      <el-table-column prop="supplierName" label="商品来源(供应商)" width="300" align="center"></el-table-column>
      <el-table-column prop="amount" label="当前库存数量(件)" width="260" align="center"></el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px; text-align: right;"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Stock",
  data() {
    return {
      searchForm: {
        stockName: "",
        productName: "",
        companyName: ""
      },
      tableData: [
        {
          id: "",
          image: "",
          productName: "",
          warehouseName: "",
          supplierName: "",
          amount: ""
        }
      ],
      currentPage: 1,
      pageSize: 7,
      total: 31
    };
  },
  methods: {
    //初始化表格
    initTable() {
      Object.assign(this.searchForm, {"currentPage": this.currentPage, "pageSize": this.pageSize});
      console.log(this.searchForm);
      this.$axios.post("http://localhost:8090/stock-details/getAllStockDetails", this.searchForm).then(({ data }) => {
        console.log(data);
        this.tableData = data.map(item => ({
          id: item.id,
          image: item.product.imgLink,
          productName: item.product.name,
          warehouseName: item.stock.stockName,
          supplierName: item.supplier.companyName,
          amount: item.amount
        }));
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '初始化失败!'
        });
      });
    },
    //条件查询
    onSearch(){

    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    }
  },
  mounted() {
    this.initTable();
  }
};
</script>

<style scoped>
/* 添加一些样式以确保页面美观 */
.demo-form-inline {
  margin-bottom: 20px;
}
.el-table {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.el-table >>> .el-table__header tr th {
  background-color: lightgray;
  color: black;
}
</style>

